package com.zhongxi.guoyuan.view;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;

public class PaySuccessActivity extends BaseActivity implements View.OnClickListener {


    private int status;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_pay_success);
        initTitle();
        tvTitle.setText("支付成功");
        status = getIntent().getIntExtra("status", 0);
    }

    @Override
    protected void initData() {
        super.initData();
        if(status == 1){
            tvTitle.setText("支付成功");
            if(null!=MyApplication.getInstance().getPay()&&!"".equals(MyApplication.getInstance().getPay())){
                if("beauty".equals(MyApplication.getInstance().getPay())){
                    showPopupWindow();
                }else if(MyApplication.getInstance().getPay().equals("reward")) {
                    showRewardCoin();
                }else{
                    showSuperWindow();
                }
            }
        }else{
            tvTitle.setText("支付失败");
            if(null!=MyApplication.getInstance().getPay()&&!"".equals(MyApplication.getInstance().getPay()))
            if("beauty".equals(MyApplication.getInstance().getPay())){
                showFailPopupWindow();
            }else if("reward".equals(MyApplication.getInstance().getPay())) {
                showFailRewardWindow();
            }else{
                showSuperFailWindow();
            }
        }
    }

    /**
     * 返银支付失败
     */
    private void showFailRewardWindow() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_fail, null);
        final AlertDialog dialog = new AlertDialog.Builder(PaySuccessActivity.this).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        ImageView ivStep = (ImageView) layout.findViewById(R.id.ivStep);
        ivStep.setImageResource(R.mipmap.reward_fail);
        TextView btSure = (TextView) layout.findViewById(R.id.btSure);
        btSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent intent = new Intent(PaySuccessActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * 返银支付成功
     */
    private void showRewardCoin() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_success, null);
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        TextView tvContent = (TextView) layout.findViewById(R.id.tvContent);
        ImageView ivStep = (ImageView) layout.findViewById(R.id.ivStep);
        ivStep.setImageResource(R.mipmap.reward_success);
        tvContent.setText("支付已成功，待您确认此项目，点击“已操作”后，即可获得:");
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent intent = new Intent(PaySuccessActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * 权益会员成功
     */
    private void showSuperWindow() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_superman_success, null);
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent intent = new Intent(PaySuccessActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * 权益会员支付失败
     */
    private void showSuperFailWindow() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_superman_fail, null);
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent intent = new Intent(PaySuccessActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * 美容院支付失败
     */
    private void showFailPopupWindow() {
            LayoutInflater inflater = LayoutInflater.from(this);
            LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_fail, null);
            final AlertDialog dialog = new AlertDialog.Builder(context).create();
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
            dialog.getWindow().setContentView(layout);
            TextView btSure = (TextView) layout.findViewById(R.id.btSure);
            btSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    startAct(MainHomeActivity.class);
                    finish();
                }
            });
    }

    /**
     * 弹出支付成功
     */
    private void showPopupWindow() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_success, null);
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        TextView btSure = (TextView) layout.findViewById(R.id.btSure);
        btSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                startAct(MainHomeActivity.class);
                finish();
            }
        });
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                startAct(MainHomeActivity.class);
                finish();
                break;
        }
    }
}
